package com.leyou.page.service.impl;

import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.service.GoodsPageService;
import com.leyou.page.vo.*;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 虎哥
 */
@Service
public class GoodsPageServiceImpl implements GoodsPageService {

    private final ItemClient itemClient;
    private final StringRedisTemplate redisTemplate;

    private static final String KEY_PREFIX_SPU = "page:spu:id:";
    private static final String KEY_PREFIX_SKU = "page:sku:id:";
    private static final String KEY_PREFIX_DETAIL = "page:detail:id:";
    private static final String KEY_PREFIX_CATEGORY = "page:category:id:";
    private static final String KEY_PREFIX_BRAND = "page:brand:id:";
    private static final String KEY_PREFIX_SPEC = "page:spec:id:";

    public GoodsPageServiceImpl(ItemClient itemClient, StringRedisTemplate redisTemplate) {
        this.itemClient = itemClient;
        this.redisTemplate = redisTemplate;
    }

    @Override
    public String loadSpuData(Long spuId) {
        // 查询商品spu信息
        SpuDTO spuDTO = itemClient.querySpuById(spuId);
        SpuVO spuVO = new SpuVO();
        spuVO.setId(spuDTO.getId());
        spuVO.setBrandId(spuDTO.getBrandId());
        spuVO.setCategoryIds(spuDTO.getCategoryIds());
        spuVO.setName(spuDTO.getName());
        // 序列化为json
        String json = JsonUtils.toJson(spuVO);
        // 写入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SPU + spuId, json);
        return json;
    }

    @Override
    public String loadSpuDetailData(Long spuId) {
        // 查询商品spu信息
        SpuDetailDTO detailDTO = itemClient.querySpuDetailById(spuId);
        // 序列化为json
        String json = JsonUtils.toJson(detailDTO);
        // 写入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_DETAIL + spuId, json);
        return json;
    }

    @Override
    public String loadSkuListData(Long spuId) {
        // 查询商品spu信息
        List<SkuDTO> dtoList = itemClient.querySkuBySpuId(spuId);
        // 序列化为json
        String json = JsonUtils.toJson(dtoList);
        // 写入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SKU + spuId, json);
        return json;
    }

    @Override
    public String loadCategoriesData(List<Long> ids) {
        if(ids.size() != 3){
            throw new LyException(400, "请求参数有误。");
        }
        // 查询商品
        List<CategoryDTO> dtoList = itemClient.queryCategoryByIds(ids);
        // 转换
        List<CategoryVO> list = dtoList.stream()
                .map(categoryDTO -> categoryDTO.toEntity(CategoryVO.class)).collect(Collectors.toList());
        // 序列化为json
        String json = JsonUtils.toJson(list);
        // 写入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_CATEGORY + ids.get(2), json);
        return json;
    }

    @Override
    public String loadBrandData(Long brandId) {
        // 查询品牌
        BrandDTO brandDTO = itemClient.queryBrandById(brandId);
        // 转换
        BrandVO brandVO = brandDTO.toEntity(BrandVO.class);
        // 序列化为json
        String json = JsonUtils.toJson(brandVO);
        // 写入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_BRAND + brandId, json);
        return json;
    }

    @Override
    public String loadSpecData(Long categoryId) {
        // 查询规格组及组内参数
        List<SpecGroupDTO> dtoList = itemClient.querySpecList(categoryId);
        // 转换vo
        List<SpecGroupVO> voList = new ArrayList<>();
        for (SpecGroupDTO groupDTO : dtoList) {
            SpecGroupVO groupVO = new SpecGroupVO();
            voList.add(groupVO);
            groupVO.setName(groupDTO.getName());
            List<SpecParamVO> paramVOList = groupDTO.getParams()
                    .stream().map(paramDTO -> paramDTO.toEntity(SpecParamVO.class)).collect(Collectors.toList());
            groupVO.setParams(paramVOList);
        }
        // 序列化
        String json = JsonUtils.toJson(voList);
        // 写入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SPEC + categoryId, json);
        return json;
    }

    @Override
    public void deleteSkuList(Long spuId) {
        redisTemplate.delete(KEY_PREFIX_SKU + spuId);
    }
}
